package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.School;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.mapper.SchoolMapper;
import com.ruoyi.system.service.ISchoolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SchoolServiceImpl implements ISchoolService {
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Resource
    private SchoolMapper schoolMapper;


    /**
     * 根据条件分页查询学校数据
     *
     * @param school 学校信息
     * @return 学校数据集合信息
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<School> selectSchoolList(School school) {
        return schoolMapper.selectSchoolList(school);
    }


    /**
     * 查询所有学校
     *
     * @return 学校列表
     */
    @Override
    public List<School> selectSchoolAll() {
        return SpringUtils.getAopProxy(this).selectSchoolList(new School());
    }


    /**
     * 通过学校ID查询学校
     *
     * @param schoolId 学校ID
     * @return 学校对象信息
     */
    @Override
    public School selectSchoolById(Long schoolId) {
        return schoolMapper.selectSchoolById(schoolId);
    }


    @Override
    public void checkSchoolAllowed(School School) {

    }





    /**
     * 新增保存学校信息
     *
     * @param school 学校信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSchool(School school) {
        // 新增学校信息
        return schoolMapper.insertSchool(school);
    }

    /**
     * 修改保存学校信息
     *
     * @param school 学校信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSchool(School school) {
        // 修改学校信息
        schoolMapper.updateSchool(school);
    }

    /**
     * 修改学校状态
     *
     * @param school 学校信息
     * @return 结果
     */
    @Override
    public int updateSchoolStatus(School school) {
        return schoolMapper.updateSchool(school);
    }

    /**
     * 修改数据权限信息
     *
     * @param school 学校信息
     * @return 结果
     */
    @Override
    @Transactional
    public int authDataScope(School school) {
        // 修改学校信息
        return schoolMapper.updateSchool(school);
    }

    @Override
    public int deleteSchoolByIds(Long[] SchoolIds) {
        return schoolMapper.deleteSchoolByIds(SchoolIds);
    }

    @Override
    public String importSchool(List<School> list, boolean updateSupport, String operaName) {
        if (StringUtils.isNull(list)) {
            throw new ServiceException("导入学校数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (School School : list) {
            try {
                School.setCreateBy(operaName);
                schoolMapper.insertSchool(School);
                successNum++;
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、学校 " + School.getSchoolName() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}
